package com.jike.jkgame;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.AbstractWheelAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jike.jkgame.data.General;
import com.jike.jkgame.data.ResourceType;
import com.jike.jkgame.data.UserResource;
import com.jike.jkgame.db.ResourcesDAO;
import com.jike.jkgame.network.data.ResourceResultData;

public class SelectResourcesActicity extends Activity implements
		OnClickListener {

	private LayoutInflater mInflater;
	private ListView mResourcesLv;
	private WheelView mCategoryWheel;
	private CategorySlotAdapter mCategoryAdaper = new CategorySlotAdapter();
	private ResourceType mResType = ResourceType.General;
	private final String mCategories[] = Utilis
			.getResStringArrary(R.array.resource_type);
	private ProgressBar mConectingPb;
	private ResourcesAdapter mResAdapter;

	private UserResource[] mResources;

	// Wheel scrolled listener
	private OnWheelScrollListener mSlotScrolledListener = new OnWheelScrollListener() {
		public void onScrollingStarted(WheelView wheel) {
		}

		public void onScrollingFinished(WheelView wheel) {

		}
	};

	// Wheel changed listener
	private OnWheelChangedListener mSlotChangedListener = new OnWheelChangedListener() {
		public void onChanged(WheelView wheel, int oldValue, int newValue) {
			mResType = ResourceType.values()[newValue];
			mCategoryWheel.requestLayout();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mInflater = LayoutInflater.from(this);
		setContentView(R.layout.select_resources_layout);

		Intent intent = getIntent();
		mResType = (ResourceType) intent.getSerializableExtra("res_type");

		mConectingPb = (ProgressBar) findViewById(R.id.pb_conecting);
		mResourcesLv = (ListView) findViewById(R.id.list);
		mResAdapter = new ResourcesAdapter();
		mResourcesLv.setAdapter(mResAdapter);

		mCategoryWheel = (WheelView) findViewById(R.id.slot_category);
		findViewById(R.id.iv_back).setOnClickListener(this);

		mCategoryWheel.setViewAdapter(mCategoryAdaper);
		mCategoryWheel.setCurrentItem(mResType.ordinal());
		mCategoryWheel.setCyclic(true);
		mCategoryWheel.setEnabled(true);
		mCategoryWheel.setVisibleItems(3);
		mCategoryWheel.addScrollingListener(mSlotScrolledListener);
		mCategoryWheel.addChangingListener(mSlotChangedListener);
		mCategoryWheel.setEnabled(false);

		GetResourceListTask task = new GetResourceListTask();
		task.execute(mResType);
	}

	private int getResourcesCount() {
		if (mResources == null) {
			return 0;
		}
		return mResources.length;
	}

	class ResourcesAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return getResourcesCount();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null ) {
				v = mInflater.inflate(R.layout.generals_item_layout, null,
						false);
				ItemViewHolder h = new ItemViewHolder();
				h.headIv = (ImageView) v.findViewById(R.id.iv_head);
				h.info1Tv = (TextView) v.findViewById(R.id.tv_info_1);
				h.info2Tv = (TextView) v.findViewById(R.id.tv_info_2);
				h.selectBtn = (Button) v.findViewById(R.id.btn_select);
				v.setTag(h);
			}
			
			ItemViewHolder holder = (ItemViewHolder) v.getTag();
			UserResource res = mResources[position];
			
			// FIXME 这里应该从资源数据里面读取相应的值
			ResourcesDAO resDao = ResourcesDAO.get();
			General general = resDao.getGeneral(res.tid);
			String info1 = String.format(
					Utilis.getResString(R.string.general_info1_format),
					String.valueOf(general.country), general.name, res.level);
			String info2 = String.format(
					Utilis.getResString(R.string.general_info2_format),
					res.attack, res.defense);
			holder.info1Tv.setText(info1);
			holder.info2Tv.setTag(info2);
			// holder.selectBtn.setEnabled(res.cast == 0);
			holder.selectBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					UserResource res = mResources[position];
					Intent intent = new Intent();
					intent.putExtra(Global.Keys.RES_DATA, res);
					setResult(Activity.RESULT_OK, intent);
					finish();
				}
			});
			return v;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;

		default:
			break;
		}

	}

	private class CategorySlotAdapter extends AbstractWheelAdapter {

		@Override
		public int getItemsCount() {
			return mCategories.length;
		}

		final LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			TextView tv;
			if (cachedView != null) {
				tv = (TextView) cachedView;
			} else {
				tv = new TextView(JKApp.get());
				tv.setGravity(Gravity.CENTER);
			}

			if (index == mResType.ordinal()) {
				tv.setTextSize(20);
			} else {
				tv.setTextSize(16);
			}
			tv.setText(mCategories[index]);
			tv.setLayoutParams(params);
			return tv;
		}
	}

	private class GetResourceListTask extends GetResourcesTask {

		@Override
		protected void onPostExecute(ResourceResultData result) {
			super.onPostExecute(result);
			mConectingPb.setVisibility(View.GONE);
			if (result != null && result.opcode == 0) {
				mResources = result.res;
			} else {
				mResources = null;
			}
			mResAdapter.notifyDataSetChanged();
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			mConectingPb.setVisibility(View.VISIBLE);
		}

	}

	private static class ItemViewHolder {
		public ImageView headIv;
		public TextView info1Tv;
		public TextView info2Tv;
		public Button selectBtn;
	}
}
